package com.yaorange.certificate.service.impl;

import com.yaorange.certificate.domain.NjtUnitConfig;
import com.yaorange.certificate.domain.NjtUserPerfect;
import com.yaorange.certificate.service.INjtUnitConfigService;
import com.yaorange.certificate.service.INjtUserPerfectService;
import com.yaorange.certificate.vo.NjtCertificateVo;
import com.yaorange.common.constant.NjtConstants;
import com.yaorange.common.core.domain.entity.SysUser;
import com.yaorange.common.core.domain.model.LoginUser;
import com.yaorange.common.enums.SysUserTypeEnum;
import com.yaorange.common.exception.ServiceException;
import com.yaorange.common.utils.DictUtils;
import com.yaorange.common.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/7/19
 */
@Service
public class NjtCommonService {
    @Autowired
    private INjtUserPerfectService userPerfectService;
    @Autowired
    private INjtUnitConfigService njtUnitConfigService;

    /**
     * 获取单位名称
     *
     * @param loginUser 登录用户
     * @return 单位名称
     */
    public String getUnitName(LoginUser loginUser) {
        String result = null;
        SysUser user = loginUser.getUser();
        // 获取当前登录用户的会员信息
        NjtUserPerfect perfect = userPerfectService.selectNjtUserPerfectById(user.getUserId());
        if (ObjectUtils.isEmpty(perfect)) {
            //获取默认的 数据字典中的 公司名称
            result = DictUtils.getDictValue(NjtConstants.SYS_CONFIG , NjtConstants.SYS_NAME);
        } else {
            // 获取当前登录用户的单位配置信息
            NjtUnitConfig unitConfig = njtUnitConfigService.selectNjtUnitConfigById(perfect.getUnitId());
            if (ObjectUtils.isNotEmpty(unitConfig)) {
                result = unitConfig.getUnit();
            }
        }
        //还是没有，就用默认公司名字
        if (StringUtils.isBlank(result)) {
            result = NjtConstants.DEFAULT_COMPANY_NAME;
        }
        return result;
    }

    /**
     * 检查用户所在单位里是否开启指定的配置的权限
     *
     * @param sysUser    当前登录用户
     * @param configType 配置项
     * @return false 无，true 有
     */
    public boolean hasConfigAuth(LoginUser sysUser, String configType) {
        NjtUserPerfect perfect = userPerfectService.selectNjtUserPerfectById(sysUser.getUserId());
        if (null == perfect) {
            return false;
        }
        Long unitId = perfect.getUnitId();

        if (null == unitId) {
            return false;
        }
        NjtUnitConfig unitConfig = njtUnitConfigService.selectNjtUnitConfigById(unitId);
        if (StringUtils.isEmpty(unitConfig.getConfigItem())) {
            return false;
        }
        // 比如：1,2,3,4,5,6,7,8 是否包含 1
        return unitConfig.getConfigItem().contains(configType);
    }
    /**
     * 根据用户，组装筛选条件 ---合格证打印列表
     * 普通用户：查看自己的
     * 监管者：查看监管范围内的
     * 管理员：查询所有
     *
     * @param njtCertificate 查询参数对象
     */
    @Transactional(readOnly = true)
    public void getAuthNjtCertificate(NjtCertificateVo njtCertificate) {
        // 获取当前登录用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String userType = sysUser.getUserType();
        if (SysUserTypeEnum.ADMIN.CODE.equals(userType)) {
            // 管理员查询所有，直接放行
            return;
        }
        if (SysUserTypeEnum.COMMON.CODE.equals(userType)) {
            // 如果是普通用户,只查看自己的
            njtCertificate.setUserId(sysUser.getUserId());
        } else if (SysUserTypeEnum.REGULATOR.CODE.equals(userType)) {
            NjtUserPerfect perfect = userPerfectService.selectNjtUserPerfectById(sysUser.getUserId());

            if (null == perfect.getUnitId()) {
                // 单位配置信息获取失败，无查看权限
                throw new ServiceException("单位信息获取失败，请确认所属单位是否有误");
            }
            // 监管者查询监管范围内的数据
            NjtUnitConfig unitConfig = njtUnitConfigService.selectNjtUnitConfigById(perfect.getUnitId());
            if (null == unitConfig) {
                // 单位配置信息获取失败，无查看权限
                throw new ServiceException("单位信息获取失败，请确认所属单位是否有误");
            } else {
                // 设置能查看的区域数据
                NjtUserPerfect njtUserPerfect = new NjtUserPerfect();
                njtUserPerfect.setProvince(unitConfig.getProvince());
                njtUserPerfect.setCity(unitConfig.getCity());
                njtUserPerfect.setArea(unitConfig.getArea());
                njtCertificate.setUserPerfect(njtUserPerfect);
            }
        } else {
            throw new ServiceException("无查看权限");
        }
    }
}
